﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace Asion.Dapper
{
    /// <summary>
    /// db congiguration
    /// </summary>
    public class DbConfiguration
    {



        /// <summary>
        /// default db type
        /// </summary>
        public static readonly DatabaseType default_dbtype = DatabaseType.SQLServer;

        /// <summary>
        /// connection string
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// db type
        /// </summary>
        public DatabaseType DbType { get; set; }

        /// <summary>
        /// db ID
        /// <para>App.config connectionStrings name</para>
        /// </summary>
        public string Id { get; set; }





        /// <summary>
        /// create id
        /// </summary>
        /// <returns></returns>
        public static string CreatId()
        {
            Guid guid = Guid.NewGuid();
            return guid.ToString();
        }

        /// <summary>
        /// get db config by  xml  config 
        /// <para>dbtype was decided by ProviderName</para>
        /// <param>defalut dbtype is SQLServer if providername is not  <param>
        /// </summary>
        public static DbConfiguration GetDbConfigurationByXmlConfig(string name)
        {
            var connectionString = ConfigurationManager.ConnectionStrings[name];
            if (connectionString == null)
            {
                throw new ArgumentNullException($"{name} Connection  is  null");
            }
            var result = new DbConfiguration()
            {
                Id = connectionString.Name,
                ConnectionString = connectionString.ConnectionString
            };
            result.DbType = RecognizeByString(connectionString.ProviderName);
            return result;
        }


        /// <summary>
        /// recognize dbtype by string
        /// </summary>
        /// <param name="dbtype"></param>
        /// <returns></returns>
        private static DatabaseType RecognizeByString(string dbtype)
        {
            DatabaseType result = default_dbtype;
            if (string.IsNullOrWhiteSpace(dbtype))
            {
                return result;
            }
            if (dbtype.Equals(DatabaseType.MySQL.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                result = DatabaseType.MySQL;
            }
            return result;
        }
        /// <summary>
        /// get db Configuration by connectionstring
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        public static DbConfiguration GetDbConfigurationByConnectionString(string connectionString, string dbType = "")
        {
            var id = CreatId();
            var result = new DbConfiguration()
            {
                Id = id,
                ConnectionString = connectionString
            };

            result.DbType = RecognizeByString(dbType);
            return result;
        }


        public static IList<DbConfiguration> GetDbConfigurationByXmlConfig()
        {
            var result = new List<DbConfiguration>();
            var connectionStrings = ConfigurationManager.ConnectionStrings;
            foreach (ConnectionStringSettings item in connectionStrings)
            {
                var model = new DbConfiguration()
                {
                    Id = item.Name,
                    ConnectionString = item.ConnectionString
                };
                model.DbType = default_dbtype;
                if (item.ProviderName.Equals(DatabaseType.MySQL.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    model.DbType = DatabaseType.MySQL;
                }
                result.Add(model);
            }
            return result;

        }










    }
}
